{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9a22bd77-6cd4-41ec-a0b8-1140c1b36cb0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import time,json\n",
    "from cubestudio.request.model_client import Client,init\n",
    "from cubestudio.train.task import InferenceService,Project"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1f1bb79c-232e-4e77-9590-5de797a453ba",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初始化验证成功\n"
     ]
    }
   ],
   "source": [
    "\n",
    "HOST = \"http://kubeflow-dashboard.infra/\"\n",
    "token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhZG1pbiJ9.j6-hUMaFYdSIzfc6i6TJ5DaS96Z9I78SrjxAOg-71yE'\n",
    "username='admin'\n",
    "init(host=HOST,username=username,token=token)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e596af1a-852c-4a5e-9963-7a934cafa83e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 添加一个inferenceservice\n",
    "inferenceservice = Client(InferenceService).add_or_update(\n",
    "    service_type=f'serving',\n",
    "    project=Client(Project).one(name='public'),\n",
    "    label='sdk创建inference service',\n",
    "    model_name='nginx',\n",
    "    model_version='v202300801',\n",
    "    images='nginx',\n",
    "    resource_memory='2G',\n",
    "    resource_cpu='2',\n",
    "    resource_gpu='0',\n",
    "    min_replicas='1',\n",
    "    max_replicas='1',\n",
    "    ports='80',\n",
    "    volume_mount='kubeflow-user-workspace(pvc):/mnt',\n",
    "    working_dir='',\n",
    "    command='',\n",
    "    env='',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2c0458bf-4a04-438f-98f8-53c300049995",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"service_type\": \"serving\",\n",
      "    \"project\": {\n",
      "        \"created_by\": null,\n",
      "        \"changed_by\": null,\n",
      "        \"created_on\": \"2023-08-06T19:47:34+00:00\",\n",
      "        \"changed_on\": \"2023-08-06T19:47:34+00:00\",\n",
      "        \"name\": \"public\",\n",
      "        \"describe\": \"\\u516c\\u5171\\u9879\\u76ee\\u7ec4\",\n",
      "        \"type\": \"org\",\n",
      "        \"expand\": \"{}\",\n",
      "        \"user\": null\n",
      "    },\n",
      "    \"name\": \"nginx-202300801\",\n",
      "    \"label\": \"sdk\\u521b\\u5efainference service\",\n",
      "    \"model_name\": \"nginx\",\n",
      "    \"model_version\": \"v202300801\",\n",
      "    \"images\": \"nginx\",\n",
      "    \"model_path\": \"/mnt/admin/cube-studio/aihub/deep-learning/cv-tinynas-object-detection-damoyolo/model/202308201835/\",\n",
      "    \"volume_mount\": \"kubeflow-user-workspace(pvc):/mnt\",\n",
      "    \"sidecar\": \"\",\n",
      "    \"working_dir\": \"\",\n",
      "    \"command\": \"\",\n",
      "    \"env\": \"\",\n",
      "    \"resource_memory\": \"2G\",\n",
      "    \"resource_cpu\": \"2\",\n",
      "    \"resource_gpu\": \"0\",\n",
      "    \"min_replicas\": 1,\n",
      "    \"max_replicas\": 1,\n",
      "    \"ports\": \"80\",\n",
      "    \"inference_host_url\": \"<a target=_blank href=\\\"http://nginx-202300801.service.local.com/gradio/aihub/cv-tinynas-object-detection-damoyolo\\\">http://nginx-202300801.service.local.com/gradio/aihub/cv-tinynas-object-detection-damoyolo</a>\",\n",
      "    \"hpa\": \"\",\n",
      "    \"priority\": 1,\n",
      "    \"canary\": \"\",\n",
      "    \"shadow\": \"\",\n",
      "    \"health\": \"\",\n",
      "    \"model_status\": \"offline\",\n",
      "    \"expand\": \"{}\",\n",
      "    \"metrics\": \"\",\n",
      "    \"deploy_history\": \"\\ndeploy prod: admin 2023-08-20 18:40:00\\nclear: admin 2023-08-22 21:08:26\",\n",
      "    \"host\": \"/gradio/aihub/cv-tinynas-object-detection-damoyolo\",\n",
      "    \"inference_config\": \"\"\n",
      "}\n",
      "推理服务发布成功，滚动升级中，注意接收升级消息\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(inferenceservice.to_dict(), indent=4))\n",
    "if inferenceservice.model_status!='online':\n",
    "    inferenceservice.deploy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c5fd24b-09dc-4511-a1c5-75ceec2fa7d5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cube-studio",
   "language": "python",
   "name": "cube-studio"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
